package com.jqb.struts2lab02.interceptor;

import java.util.Map;

import com.jqb.sturts2lab02.action.LoginAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LimitInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;

	/**
	 * Interceptor初始化参数,配置于struts.xml中
	 */
	private String msg;
	
	@Override
	@SuppressWarnings({ "rawtypes" })
	public String intercept(ActionInvocation invocation) throws Exception {
		
		System.out.println("...trying to get interceptor param...msg:"+this.msg);
		
		System.out.println("...interceptor...strating...");
		
		/**
		 * 过滤对LoginAction的拦截,即不对LoginAction拦截
		 */
		if (LoginAction.class == invocation.getAction().getClass()) {
			System.out.println("...intercepting...1...");
			return invocation.invoke();
		}
		
		Map map = invocation.getInvocationContext().getSession();

		if (map.get("userInfo") == null) {
			System.out.println("...intercepting...2...");
			
			/**
			 * 转向login视图(在全局视图中)
			 */
			return Action.LOGIN;
		}
		System.out.println("...intercepting...3...");
		return invocation.invoke();
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}
